package com.party.admin.task;

import com.party.admin.biz.distribution.DistributionBizService;
import com.party.common.utils.StringUtils;
import com.party.core.model.distributor.DistributorCount;
import com.party.core.model.distributor.DistributorRelation;
import com.party.core.model.distributor.DistributorRelationType;
import com.party.core.service.distributor.IDistributorCountService;
import com.party.core.service.distributor.IDistributorRelationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 分销定时任务
 *
 * @author yifeng
 * @date 2018/11/15 0015
 * @time 下午 16:21
 */
@Component(value = "quartzForDistribution")
public class QuartzForDistribution {
    @Autowired
    private IDistributorCountService distributorCountService;
    @Autowired
    private DistributionBizService distributorBizService;
    @Autowired
    private IDistributorRelationService distributorRelationService;

    Logger logger = LoggerFactory.getLogger(getClass());


    public void refreshCount() {
        List<Map<String, Object>> mapList = distributorRelationService.getTargetEndTime();

        for (Map<String, Object> stringObjectMap : mapList) {
            try {
                Integer type = (Integer) stringObjectMap.get("type");
                String targetId = (String) stringObjectMap.get("targetId");
                Date endTime = (Date) stringObjectMap.get("endTime");
                if (StringUtils.isEmpty(targetId)) {
                    continue;
                }
                if (!DistributorRelationType.MALL_GOODS.getCode().equals(type) && null == endTime) {
                    continue;
                }
                if (DistributorRelationType.MALL_GOODS.getCode().equals(type)
                        || DistributorRelationType.ARTICLE.getCode().equals(type)) {
                    DistributorRelation t = new DistributorRelation();
                    t.setTargetEndDate(null);
                    t.setTargetId(targetId);
                    distributorRelationService.updateByTargetId(t);
                } else if (endTime != null) {
                    DistributorRelation t = new DistributorRelation();
                    t.setTargetEndDate(endTime);
                    t.setTargetId(targetId);
                    distributorRelationService.updateByTargetId(t);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        List<DistributorCount> list = distributorCountService.list(new DistributorCount());
        for (DistributorCount distributorCount : list) {
            try {
                DistributorRelation distributorRelation = distributorRelationService.get(distributorCount.getDistributorRalationId());
                if (null == distributorRelation || null == distributorRelation.getType()) {
                    continue;
                }
                distributorBizService.refreshCount(distributorCount, distributorRelation.getType());
            } catch (Exception e) {
                logger.error("更新分销统计异常", e);
                continue;
            }
        }
    }
}
